import sys
import os
import glob
import re
from Tkinter import *

class BatchMode:
    def __init__(self, target_dir):
        # Define all the widgets needed
        self.window = Tk()
        self.cur_dir_label = Label(self.window, text="Current Directory",
                                   justify=CENTER)
        self.affected_label = Label(self.window, text="Affected Files",
                                    justify=CENTER)
        self.dir_scroll = Scrollbar(self.window)
        self.dir_list = Listbox(self.window, yscrollcommand=self.dir_scroll.set)
        self.affected_scroll = Scrollbar(self.window)
        self.affected_list = Listbox(self.window,
                                     yscrollcommand=self.affected_scroll.set)
        self.regex_label = Label(self.window, text="Selection Expression",
                                 justify=CENTER)
        self.regex_entry = Entry(self.window)
        self.regex_find = Button(self.window, text="Find Matching Files",
                                 command=self.update_affected)
        self.apply_button = Button(self.window, text="Apply", state=DISABLED,
                                   command=self.apply_changes)
        self.exit_button = Button(self.window, text="Exit",
                                  command=self.window.quit)

        self.dir_scroll.config(command=self.dir_list.yview)
        self.affected_scroll.config(command=self.affected_list.yview)

        # Populate the current directory listing with all relevant files
        self.curdir = os.path.abspath(target_dir)
        self.piclist = [f for f in glob.glob(os.path.join(self.curdir,'*.*')) if
                            f[-3:].lower() == 'jpg' or f[-3:].lower() == 'png' or
                            f[-3:].lower() == 'gif' or f[-3:].lower() == 'bmp' or
                            f[-4:].lower() == 'jpeg']
        for i in xrange(0, len(self.piclist)):
            path, filename = os.path.split(self.piclist[i])
            self.piclist[i] = filename
            self.dir_list.insert(END, filename)

        # Define the positions in the window's manager using Tk's grid manager
        self.cur_dir_label.grid(row=0, column=0, columnspan=2)
        self.affected_label.grid(row=0, column=2, columnspan=2)
        self.dir_list.grid(row=1, column=0)
        self.dir_scroll.grid(row=1, column=1, sticky=N+S)
        self.affected_list.grid(row=1, column=2)
        self.affected_scroll.grid(row=1, column=3, sticky=N+S)
        self.regex_label.grid(row=2, columnspan=4, pady=1)
        self.regex_entry.grid(row=3, columnspan=4, pady=1)
        self.regex_find.grid(row=4, columnspan=4, pady=1)
        self.apply_button.grid(row=5, column=0, columnspan=2, pady=4)
        self.exit_button.grid(row=5, column=2, columnspan=2, pady=4)

    def apply_changes(self): pass

    def update_affected(self):
        self.affected_list.delete(0, END)
        if self.regex_entry.get() is not "":
            exp = re.compile(self.regex_entry.get())
            for pic in self.piclist:
                match_test = re.match(exp, pic)
                if match_test: self.affected_list.insert(END, pic)
        if self.affected_list.size() > 0:
            self.apply_button.config(state=NORMAL)
        else:
            self.apply_button.config(state=DISABLED)

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print "Usage: ", sys.argv[0], "<directory to modify>"
        raise SystemExit()
    batch_obj = BatchMode(sys.argv[1])
    batch_obj.window.mainloop()
